package com.roger.uia.helper.util;

import android.util.Log;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
@SuppressWarnings("ResultOfMethodCallIgnored")
public class ZipUtils {
    private static final String TAG = "Test" + ZipUtils.class.getSimpleName();

    public static ZipUtils zip(){ return new ZipUtils(); }

    /**
     * zip file
     * @param srcFile     the file that will be zipped
     * @param destFile    zipped file
     */
    public void zipFile(String srcFile, String destFile) {
        try{
            File sourceFile = new File(srcFile);
            File zipPath = new File(destFile).getParentFile();
            if (!zipPath.exists()) zipPath.mkdirs();
            File zipFile = new File(destFile);
            ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
            writeZip(sourceFile, "", zos);
            zos.close();
            FileUtils.delFolder(sourceFile);
        } catch (Exception e) {
            Log.d(TAG,"zip file fail");
            e.printStackTrace();
        }
    }

    /**
     * unzip file
     * @param srcFile       the file that will be unzipped
     * @param destDir       unzip file directory
     */
    public void unZipFile(String srcFile, String destDir){
        try {
            ZipFile zipFile = new ZipFile(srcFile);
            Enumeration<?> entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = (ZipEntry) entries.nextElement();
                if(entry.isDirectory()) {
                    String dirPath = destDir + File.separator + entry.getName();
                    new File(dirPath).mkdirs();
                }else{
                    File targetFile = new File(destDir + File.separator + entry.getName());
                    if(!targetFile.getParentFile().exists()){
                        targetFile.getParentFile().mkdirs();
                    }
                    targetFile.createNewFile();
                    InputStream is = zipFile.getInputStream(entry);
                    FileOutputStream fos = new FileOutputStream(targetFile);
                    int len;
                    byte[] buf = new byte[1024 * 10];
                    while ((len = is.read(buf)) != -1) {
                        fos.write(buf, 0, len);
                    }
                    fos.close();
                    is.close();
                }
            }
        }catch (Exception e) {
           Log.d(TAG,"unzip error");
            e.printStackTrace();
        }
    }

    /**
     *
     * @param file             src file
     * @param parentPath      parent Path
     * @param zos              zipOutputStream
     */
    private void writeZip(File file, String parentPath, ZipOutputStream zos) {
        if (file.isDirectory()){
            parentPath += file.getName() + File.separator;
            File[] files = file.listFiles();
            for (File f : files) {
                writeZip(f, parentPath, zos);
            }
        }else{
            try{
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
                ZipEntry zipEntry = new ZipEntry(parentPath + file.getName());
                zos.putNextEntry(zipEntry);
                int len;
                byte[] buffer = new byte[1024 * 10];
                while ((len = bis.read(buffer, 0, buffer.length)) != -1) {
                    zos.write(buffer, 0, len);
                    zos.flush();
                }
                bis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
